home *** CD-ROM | disk | FTP | other *** search
- Path: cymbal.aix.calpoly.edu!not-for-mail
- From: dstubbs@cymbal.aix.calpoly.edu (Dan Stubbs)
- Newsgroups: comp.lang.c
- Subject: Re: Fastest way to computer log(base2) of x?
- Date: 26 Jan 1996 10:03:23 -0800
- Organization: California Polytechnic State University, San Luis Obispo
- Message-ID: <4eb51b$1d5r@cymbal.aix.calpoly.edu>
- References: <4e61iu$p6e@villa.fc.net>
- NNTP-Posting-User: dstubbs@cymbal.aix.calpoly.edu
-
- In article <4e61iu$p6e@villa.fc.net>,
- Avinash Chopde <avinash@paranoia.com> wrote:
- >
- >I am trying to find out what could be the fastest way to compute
- >the position of the highest bit in any given integer -- basically, the
- >logarithm to the base 2 of any number.
- >
- --[text deleted]
-
- >Any thoughts or other ideas?
- >
- >Thanks!
- >
- >Avinash Chopde
- >e-mail: avinash@acm.org
- >home page: http://www.paranoia.com/~avinash/
-
- Shown below is a plot of the relative times needed to find the highest
- bit as a function of the position of the target bit, and the search
- technique. Three search techniques were used and their implementations
- are given at the end of this post. The three techniques are
- (1) sequential search one bit at a time, (2) binary search, and
- (3) sequential search four bits at a time followed by sequential
- search one bit at a time. Method (3) is fastest in almost every
- case--loosing to method (1) only when the highest bit is near the
- "left" end of the target value.
-
- Time to find the left most bit in a 32 bit target value
- as a function of the target value. The target value
- is 2**k where k is shown as the value on the x axis
- below. For example 2**20 = 1,048,576. The three search
- techniques reported are:
-
- S S = sequential search.
- B = binary search.
- 50+ L = block search.
- | S
- | The three implementations are
- | shown below.
- |
- 40+
- | S
- |
- Time
- |
- 30+ S
- |
- | B
- | B
- | B S
- 20+ L B B
- | L B
- | B
- | L S
- | L
- 10+ L L
- | L
- | S
- |
- |
- +----------------------------------- Value of k where
- 0 4 1 1 2 2 3 target value = 2**k.
- 0 5 0 5 0
-
- int lmb (int k) {
- /*
- * returns the position of the left most bit in k assuming that
- * k > 0 and 32 bit ints. The algorithm used is a binary search.
- */
- unsigned left = 0, width = 16, mask = 0xffff0000;
-
- while (width > 1) {
- if (k & mask) {
- width /= 2;
- mask <<= (left + width);
- mask >>= left;
- }
- else {
- mask >>= width;
- left += width;
- }
- }
- if (k & mask) return left;
- else return left+1;
- }
-
- int lmb (int k) {
- /*
- * returns the position of the left most bit in k assuming that
- * k > 0 and 32 bit ints. The algorithm used is a bit at a time
- * sequential search.
- */
- unsigned left = 0, mask = 0x80000000;
-
- while (!(k & mask)) {
- mask >>= 1;
- left++;
- }
- return left;
- }
-
- int lmb (int k) {
- /*
- * returns the position of the left most bit in k assuming that
- * k > 0 and 32 bit ints. The algorithm used uses two sequential
- * searchs. The first searches for the block of four bits
- * contianing the left most bit, and the second searches the
- * block found in the first search.
- */
- unsigned j=0, i=0;
- static unsigned m1[] = {0xf0000000,0x0f000000,0x00f00000,0x000f0000,
- 0x0000f000,0x00000f00,0x000000f0,0x0000000f};
- static unsigned m2[] = {0x80000000,0x08000000,0x00800000,0x00080000,
- 0x00008000,0x00000800,0x00000080,0x00000008};
- unsigned mask;
-
- while (!(m1[j] & k)) ++j;
- mask = m2[j];
- while (!(mask & k)) {
- mask >>= 1;
- ++i;
- }
- return (4*j + i);
- }
-
-